﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class DropDownList_IcmBidSeq : System.Web.UI.UserControl
    {
        public RadComboBox Child;
        int nOffset = 10;
        Main main = new Main();
        IBLL_ICM_Bid bid = new B_ICM_Bid();
        IBLL_ICM_Bidder bidder = new B_ICM_Bidder();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public RadComboBox GetComboBox()
        {
            RadComboBox ddl = ddlIcmBidSeq;
            return ddl;
        }

        public void LoadData(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            string strBgmYearSeq = (lblBgmYear.Text.Length > 0 ? lblBgmYear.Text : null);
            string strReceiveType = (lblReceiveType.Text.Length > 0 ? lblReceiveType.Text : null);
            string strStep = (lblStep.Text.Length > 0 ? lblStep.Text : null);
            string strDate = (lblDate.Text.Length > 0 ? lblDate.Text : null);
            IList<M_ICM_Bid> infolist = new List<M_ICM_Bid>();

            if (strStep == "receive")
            {
                infolist = bid.GetReceiveList(strBgmYearSeq, strDate, strFilter);
            }
            else
            {
                infolist = bid.GetOfferList(strBgmYearSeq, strDate, strFilter);
            }

            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + nOffset, infolist.Count);
            e.EndOfItems = endOffset == infolist.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                ddlIcmBidSeq.Items.Add(new RadComboBoxItem(infolist[i].icm_bid_name, infolist[i].icm_bid_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infolist.Count);
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset
            , string strBgmYearSeq, string strStep, string strDate)
        {
            LoadControl(nWidth, nSetOffset, strBgmYearSeq, "S", strStep, strDate);
        }

        public void LoadControl(Nullable<int> nWidth, Nullable<int> nSetOffset
            , string strBgmYearSeq, string strReceiveType, string strStep, string strDate)
        {
            if (nWidth != null)
                ddlIcmBidSeq.Width = Unit.Pixel(nWidth.Value);

            if (nSetOffset != null)
                nOffset = nSetOffset.Value;

            lblBgmYear.Text = strBgmYearSeq;
            lblReceiveType.Text = strReceiveType;
            lblStep.Text = strStep;
            lblDate.Text = strDate;
        }

        public void SetSelectedValue(string strValue, string strContactSeq)
        {
            if (strValue.Length > 0)
            {
                lblIcmBidSeq.Text = strValue;

                if (ddlIcmBidSeq.FindItemByValue(strValue) == null)
                {
                    M_ICM_Bid info = bid.GetInfo(strValue);
                    ddlIcmBidSeq.Text = info.icm_bid_name;
                }
                else
                {
                    ddlIcmBidSeq.SelectedValue = strValue;
                }
            }
            else
            {
                ddlIcmBidSeq.SelectedIndex = -1;
                ddlIcmBidSeq.Text = "";
                lblIcmBidSeq.Text = "";
            }

            if (strContactSeq != null)
            {
                lblContactSeq.Text = strContactSeq;
            }
            else
            {
                lblContactSeq.Text = "";
            }
        }

        public string GetSelectedValue()
        {
            string strValue = lblIcmBidSeq.Text;

            return strValue;
        }

        public void ddlIcmBidSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strValue = ddlIcmBidSeq.SelectedValue;
            lblIcmBidSeq.Text = strValue;

            if (Child != null)
            {
                Child.Text = "";

                if (strValue.Length > 0)
                {
                    switch (Child.ID)
                    {
                        case "ddlContactSeq" :
                            string ID = this.ID;
                            App_UI.UserControls.DropDownList_Contact uc = (App_UI.UserControls.DropDownList_Contact)Child.Parent;

                            switch (ID)
                            {
                                case "DropDownList_IcmBidSeqReceive" :
                                    uc.LoadControl(null, null);

                                    break;
                                case "DropDownList_IcmBidSeqOffer" :
                                    uc.LoadControl(null, null, "Offer", strValue);
                                    M_ICM_Bid bidInfo = bid.GetInfo(strValue);

                                    ((RadNumericTextBox)Parent.FindControl("txtAmount")).Text = bidInfo.document_amount.ToString();
                                    //((RadNumericTextBox)Parent.FindControl("txtBathAmount")).Text = bidInfo.document_amount.ToString();
                                    break;
                            }

                            break;
                    }
                }
                else
                {
                    Child.DataSource = null;
                    Child.DataBind();
                }
            }
        }

        protected void ddlIcmBidSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadData(e);
        }
    }
}